﻿using System.Xml.Serialization;
using ALC.Core.Enums;
using MeasurementCommon.Fai;
using WPFCommon.ViewModels.Base;

namespace ALC.Core.ViewModels.Workpiece
{
    public sealed partial class FaiItem : AutoSerializableBase<FaiItem>
    {
        #region attribute

        /// <summary>
        /// Max boundary of the fai item
        /// </summary>
        [XmlAttribute]public double MaxBoundary { get; set; }

        /// <summary>
        /// Min boundary of the fai item
        /// </summary>
        [XmlAttribute]public double MinBoundary { get; set; }
        [XmlAttribute]public double Standard { get; set; }
        
        [XmlAttribute] public double Weight { get; set; } = 1;

        /// <summary>
        /// Bias 
        /// </summary>
        [XmlAttribute] public double Bias { get; set; }

        #endregion

        #region ignore

        /// <summary>
        /// Measured value
        /// </summary>
        [XmlIgnore]
        public double ValueUnbiased { get; set; }

        /// <summary>
        /// Measured value plus bias
        /// </summary>
        [XmlIgnore]
        public double Value =>ValueUnbiased * Weight + Bias;
        
             
        [XmlIgnore]
        public ValueState ValueState
        {
            get
            {
                if (Value > MaxBoundary) return ValueState.TooLarge;
                if (Value < MinBoundary) return ValueState.TooSmall;
                return ValueState.Normal;
            }
        }



        #endregion
        
        public FaiItem Clone()
        {
            return new FaiItem()
            {
                Name = Name,
                MinBoundary = MinBoundary,
                MaxBoundary = MaxBoundary,
                Standard = Standard,
                ValueUnbiased = ValueUnbiased,
                Weight = Weight,
                Bias = Bias,
            };
        }

    }
}